package com.syezon.wifikey.wifi;

import android.arch.lifecycle.Observer;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.syezon.wifikey.R;
import com.syezon.wifikey.base.BaseActivity;
import com.syezon.wifikey.wifi.adapter.BlackWifiAdapter;
import com.syezon.wifikey.pwd.dialog.RemoveBlackDialog;
import com.syezon.wifikey.utils.StatusBarUtil;
import com.syezon.wifikey.wifi.info.BlackWifiInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BlackListActivity extends BaseActivity implements BlackWifiAdapter.BlackItemClickListener, RemoveBlackDialog.RemoveBlackCallBack {

    @BindView(R.id.rl_top)
    RelativeLayout mRlTop;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_wifi_list)
    RecyclerView mRvList;
    private BlackWifiAdapter mBlackWifiAdapter;
    private RemoveBlackDialog removeBlackDialog;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_black_list;
    }

    @OnClick({R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setPaddingSmart(this, mRlTop);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        mBlackWifiAdapter = new BlackWifiAdapter(this);
        mBlackWifiAdapter.setListener(this);
        mRvList.setAdapter(mBlackWifiAdapter);
        BlackWifiService.getInstance().getBlackWifiLiveData().observe(this, new Observer<List<BlackWifiInfo>>() {
            @Override
            public void onChanged(@Nullable List<BlackWifiInfo> blackWifiInfos) {
                if (blackWifiInfos.size() > 0) {
                    toggleShowLoading(false, "");
                } else {
                    showView(LayoutInflater.from(BlackListActivity.this).inflate(R.layout.layout_empty_black, null, false), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BlackWifiService.getInstance().getBlackWifiList();
                        }
                    });
                }
                mBlackWifiAdapter.setData(blackWifiInfos);
            }
        });
        BlackWifiService.getInstance().getBlackWifiList();
    }

    @Override
    protected View getLoadingTargetView() {
        return mRefreshLayout;
    }

    @Override
    public void onBlackItemClicked(BlackWifiInfo blackWifiInfo) {
        showBlackRemoveConfirmDialog(blackWifiInfo);
    }

    private void showBlackRemoveConfirmDialog(BlackWifiInfo blackWifiInfo) {
        if (removeBlackDialog == null) {
            removeBlackDialog = new RemoveBlackDialog(this, R.style.Dialog);
            removeBlackDialog.setRemoveCallBack(this);
        }
        removeBlackDialog.setBlackInfo(blackWifiInfo);
        removeBlackDialog.show();
    }

    @Override
    public void onBlackRemoved(BlackWifiInfo blackWifiInfo) {
        List<BlackWifiInfo> data = mBlackWifiAdapter.getData();
        int indexOf = data.indexOf(blackWifiInfo);
        if (indexOf >= 0) {
            data.remove(blackWifiInfo);
            mBlackWifiAdapter.notifyItemRemoved(indexOf);
            BlackWifiService.getInstance().deleteBlackWifi(blackWifiInfo);
        }
        if (data.size() == 0) {
            showView(LayoutInflater.from(BlackListActivity.this).inflate(R.layout.layout_empty_black, null, false), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BlackWifiService.getInstance().getBlackWifiList();
                }
            });
        }
    }
}
